package kr.saeildamil.androidstudty.ch12;

import java.util.ArrayList;

import kr.saeildamil.androidstudty.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;

public class ListAddDel extends Activity {
	ArrayList<String>arList;
	ArrayAdapter<String>adapter;
	ListView list;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch12_listadddel);
		
		arList=new ArrayList<String>();
		arList.add("first");
		arList.add("second");
		arList.add("third");
		
		adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice,arList);
		
		list=(ListView) findViewById(R.id.ch12_listadddel_list);
		list.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		list.setAdapter(adapter);
		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position,
					long arg3) {
				Toast.makeText(ListAddDel.this, arList.get(position), Toast.LENGTH_SHORT).show();	
			}
		});
	}
	
	public void mOnClick(View v){
		EditText et=(EditText) findViewById(R.id.ch12_listadddel_edit);
		switch (v.getId()) {
		case R.id.ch12_listadddel_addbtn:
			String str=et.getText().toString();
			arList.add(str);
			adapter.notifyDataSetChanged();
			et.setText("");
			break;

		case R.id.ch12_listadddel_del:
			int pos=list.getCheckedItemPosition();
			if(pos!=ListView.INVALID_POSITION){
				arList.remove(pos);
				list.clearChoices();
				adapter.notifyDataSetChanged();
			}
			break;
		}
	}
}
